package org.jeecg.modules.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.CrawlComponent;
import org.jeecg.modules.system.service.IScrawlComponentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author wt
 * @Date: 2024/9/25 10:49
 * @Description:
 */
@RestController
@RequestMapping("/sys/CrawlComponent")
@Slf4j
public class CrawlComponentController {
    @Autowired
    private IScrawlComponentService iScrawlComponentService;

    /**
     * 分页列表查询
     *
     * @param component
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<CrawlComponent>> queryPageList(CrawlComponent component,
        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
              log.info("=====查询========"+component);
        Result<IPage<CrawlComponent>> result = new Result<IPage<CrawlComponent>>();
        Page<CrawlComponent> page = new Page<CrawlComponent>(pageNo, pageSize);
        IPage<CrawlComponent> pageList = iScrawlComponentService.queryComponent(page, component);
        result.setSuccess(true);
        result.setResult(pageList);
        log.info("=====result========" + result);
        return result;
    }

    /**
     * 新增   爬虫类型中有3种 text,range,list
     *
     * @param crawlComponent
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<CrawlComponent> add(@RequestBody CrawlComponent crawlComponent) {
        Result<CrawlComponent> result = new Result<CrawlComponent>();
        try {
            log.info("=====Type======"+crawlComponent.getType());
            if (crawlComponent.getType().equals("text")){
                crawlComponent.setQueryParam(0L);
            }else if (crawlComponent.getType().equals("range")){
                crawlComponent.setQueryParam(1L);
            }else if(crawlComponent.getType().equals("list")){
                crawlComponent.setQueryParam(2L);
            }else {
                crawlComponent.setQueryParam(0L);
            }
            iScrawlComponentService.insertCrawlComponent(crawlComponent);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 编辑
     *
     * @param crawlComponent
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    public Result<CrawlComponent> edit(@RequestBody CrawlComponent crawlComponent) {
        Result<CrawlComponent> result = new Result<CrawlComponent>();
        CrawlComponent crawlComponent1 = iScrawlComponentService.getById(crawlComponent.getId());
        if (crawlComponent1 == null) {
            result.error500("没找到对应实体类");
        } else {
            try {
                if (crawlComponent.getType().equals("text")){
                    crawlComponent.setQueryParam(0L);
                }else if (crawlComponent.getType().equals("range")){
                    crawlComponent.setQueryParam(1L);
                }else if(crawlComponent.getType().equals("list")){
                    crawlComponent.setQueryParam(2L);
                }else {
                    crawlComponent.setQueryParam(0L);
                }
                log.info("======参数值======"+crawlComponent.getQueryParam());
                boolean ok = iScrawlComponentService.updateById(crawlComponent);
                if (ok) {
                    result.success("修改成功!");
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                result.error500("修改失败");
            }
        }
        return result;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<?> delete(@RequestParam(name = "id", required = true) Long id) throws IOException {
        iScrawlComponentService.deleteComponent(id);
        return Result.ok("删除成功");
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result<CrawlComponent> deleteBatch(@RequestParam Long[] ids) {
        Result<CrawlComponent> result = new Result<CrawlComponent>();
        if (oConvertUtils.isEmpty(ids)) {
            result.error500("未选中变量！");
        } else {
            iScrawlComponentService.deleteBatchComponent(ids);
            result.success("删除成功!");
        }
        return result;
    }


}
